import { Http } from '@/@nasty/Axios';
import { util } from '@/@nasty/Util';
import Upload from '@/@nasty/components/Upload';
import { PlusOutlined } from '@ant-design/icons';
import { ActionType, ModalForm, ProColumns, ProFormInstance, ProFormItem, ProFormSelect, ProFormText, ProFormTextArea, ProTable } from '@ant-design/pro-components';
import { FormattedMessage, useIntl, useRequest } from '@umijs/max';
import { Button, message } from 'antd';
import { FC, cloneElement, useCallback, useRef, useState } from 'react';

async function SaveQuartzTask(options: any) {
    return new Promise<any>((resolve, reject) => {
        Http.post("/Quartz/QuartzTask/SaveQuartzTask", { ...options })
            .then((data) => {
                if (data.IsSuccess !== true)
                    reject(data.Message);
                else
                    resolve(data);
            }).catch((e) => {
                reject(e)
            })
    });
}


const AddModel: FC<any> = (props) => {
    const { reload } = props;

    const [messageApi, contextHolder] = message.useMessage();
    /**
     * @en-US International configuration
     * @zh-CN 国际化配置
     * */
    const intl = useIntl();

    const { run, loading } = useRequest(SaveQuartzTask, {
        manual: true,
        onSuccess: () => {
            messageApi.success('提交成功');
            reload?.();
        },
        onError: (e: any) => {
            messageApi.error(e);
        },
    });

    return (
        <>
            {contextHolder}
            <ModalForm
                title={"新增定时任务"}
                trigger={
                    <Button type="primary" icon={<PlusOutlined />}>
                        {"新增"}
                    </Button>
                }
                width="552px"
                modalProps={{ okButtonProps: { loading } }}
                onFinish={async (value) => {
                    await run({ ...value });
                    return true;
                }}
            >


                <ProFormText label="名称" name="Name" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "名称必填",
                        },
                    ]}

                />

                <ProFormText label="组名称" name="Group" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "组名称必填",
                        },
                    ]}
                />


                <ProFormText label="Cron表达式" name="Cron" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "组名称必填",
                        },
                    ]}
                />

                <ProFormText label="Job类名" name="Class" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "Job类名必填",
                        },
                    ]}
                />

                <ProFormText label="备注" name="Remark" width="xl" />
            </ModalForm>
        </>
    );
};


const UpdateModel: FC<any> = (props) => {
    const { reload } = props;
    const { values, trigger } = props;
    const formRef = useRef<ProFormInstance>();
    const [messageApi, contextHolder] = message.useMessage();
    /**
     * @en-US International configuration
     * @zh-CN 国际化配置
     * */
    const intl = useIntl();
    const { run, loading } = useRequest(SaveQuartzTask, {
        manual: true,
        onSuccess: () => {
            messageApi.success('提交成功');
            reload?.();
        },
        onError: (e: any) => {
            messageApi.error(e);
        },
    });


    const load = () => {
        Http.post("/Quartz/QuartzTask/GetQuartzTask", { id: values.Id }).then((res) => {
            if (res.Id) {
                formRef.current?.setFieldsValue(res);
            } else {
                message.error("岗位数据加载失败");
            }

        }).catch((error) => {
            message.error(error);
        })

    }


    return (
        <>
            {contextHolder}
            <ModalForm
                title={"修改定时任务"}
                formRef={formRef}
                trigger={trigger}
                width="552px"
                modalProps={{ okButtonProps: { loading } }}
                onFinish={async (value) => {
                    await run({ ...value });
                    return true;
                }}
                onOpenChange={(v) => {
                    if (v) load();
                }}


            >
                <ProFormText hidden={true} name="Id" />


                <ProFormText label="名称" name="Name" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "名称必填",
                        },
                    ]}

                />

                <ProFormText label="组名称" name="Group" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "组名称必填",
                        },
                    ]}
                />


                <ProFormText label="Cron表达式" name="Cron" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "组名称必填",
                        },
                    ]}
                />

                <ProFormText label="Job类名" name="Class" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "Job类名必填",
                        },
                    ]}
                />

                <ProFormText label="备注" name="Remark" width="xl" />
            </ModalForm>
        </>
    );
};

async function getPageApi(
    params: any,
    options?: any,
) {
    return new Promise<any>((resolve, reject) => {
        Http.post("/Quartz/QuartzTask/GetQuartzTaskLogPage", { ...params }, { isLower: true })
            .then((data) => {
                resolve(data);
            }).catch((e) => {
                reject(e)
            })
    });

}

const LogModel: FC<any> = (props) => {
    const { reload, departmentId, userId } = props;
    const [messageApi, contextHolder] = message.useMessage();
    const formRef = useRef<ProFormInstance>();
    const actionRef = useRef<ActionType>();
    const [selectedRowKeys, setSelectedRowKeys] = useState<any[]>([]);
    const [selectedRows, setSelectedRows] = useState<any[]>([]);
    /**
     * @en-US International configuration
     * @zh-CN 国际化配置
     * */
    const intl = useIntl();



    const columns: ProColumns<any>[] = [
        {
            title: "结果",
            dataIndex: 'Result',
        },
        {
            title: "创建时间",
            dataIndex: 'CreateTime',
            hideInSearch: true,
        }
    ];

    return (
        <>
            {contextHolder}
            <ModalForm
                title={"日志"}
                trigger={<a>{"日志"}</a>}

                formRef={formRef}
                onFinish={async (value) => {
                    return true;
                }}

                onOpenChange={(v) => {
                    if (v) {
                        setSelectedRowKeys([]);
                        setSelectedRows([]);
                    }
                }}
            >

                <ProTable<any, any>
                    headerTitle={"日志列表"}
                    actionRef={actionRef}
                    rowKey="Id"
                    search={{
                        labelWidth: 'auto',
                        span: 6, // 根据弹窗宽度调整
                        defaultCollapsed: true, // 默认折叠搜索区域
                    }}
                    request={(p) => { p = { TaskId: props.taskId, ...p }; return getPageApi(p, null); }}
                    columns={columns}
                />
            </ModalForm>
        </>
    );
};



export { AddModel, UpdateModel, LogModel }